/*
package com.yc.class_attendance;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.yc.class_attendance.adapter.AttendanceAdapter;
import com.yc.class_attendance.adapter.Gallery_adapter;
import com.yc.class_attendance.entity.Attachment;
import com.yc.class_attendance.entity.AttendanceResutl;
import com.yc.class_attendance.entity.ImageResutl;
import com.yc.class_attendance.entity.MeetingCheck;
import com.yc.class_attendance.entity.MeetingResutl;
import com.yc.class_attendance.entity.PersonnelResutl;
import com.yc.class_attendance.entity.Resutl;
import com.yc.class_attendance.fragment.SetDiagFragment;
import com.yc.class_attendance.net.AbsAPICallback;
import com.yc.class_attendance.net.Api;
import com.yc.class_attendance.service.CheckCardExistService;
import com.yc.class_attendance.utils.AutoText;
import com.yc.class_attendance.utils.DatetimeUtil;
import com.yc.class_attendance.utils.RealmHelper;
import com.yc.class_attendance.utils.SharePreferenceUtil;
import com.yc.nfc.CardInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import rx.Subscriber;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tiem;
    private TextView week;
    private TextView date;
    private ImageView set;
    private TextView place;
    Intent intent1;
    SurfaceView preview;
    private Camera mCamera;
    SurfaceHolder mSurfaceHolder;
    private ViewGroup mMoreLayout, mMoreLayouts;
    LinearLayout linearLayout;
    ViewGroup.LayoutParams layoutParams;
    GridView listView;
    RealmHelper realmHelper = new RealmHelper(this);
    List<MeetingCheck> list1 = new ArrayList<>();
    public List<String> class_attemdamce = new ArrayList<>();
    List<String> BigenCard = new ArrayList<>();
    List<String> EndCard = new ArrayList<>();
    List<String> CourseId = new ArrayList<>();
    List<String> UUid = new ArrayList<>();
    TextView practical;
    int indexBigen;
    int indexEnd;
    int realized = 0;
    SoundPool soundPool = null;
    SoundPool soundPool1 = null;
    SoundPool soundPool2 = null;
    int sourceid = 0;
    int failure = 0;
    int credit = 0;
    boolean isSoundReady;
    boolean subscript;
    List<PersonnelResutl> list = new ArrayList<>();
    AttendanceAdapter attendanceAdapter;
    Gallery main_gallery;
    LinearLayout main_lin;
    Gallery_adapter gallery_adapter;
    int current_circle = 0;
    Runnable timeadv;
    int count;
    ImageView imgview;
    List<String> li = new ArrayList<String>();
    private SharePreferenceUtil mSharePreferenceUtil;
    Timer timerImgView;
    TimerTask taskImgView;
    Bitmap bitmap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        RxBus.get().register(this);
        intent1 = new Intent(this, CheckCardExistService.class);
        startService(intent1);
        mSharePreferenceUtil = MyApp.getsInstance().sharePreferenceUtil;
        setView();
        setTiem();
        message();
        personnel();
        SetImagView();

        //定时器
        Timer timerCard = new Timer();
        TimerTask tastCard = new TimerTask() {
            @Override
            public void run() {
                if (MyApp.getsInstance().getgiDestPort() == 1) {
                    MyApp.getsInstance().setCard("1");
                }


            }
        };
        timerCard.schedule(tastCard, 1000, 1000);
    }


    private void setTiem() {
        tiem = getView(R.id.tiem);
        week = getView(R.id.week);
        date = getView(R.id.date);
        SetData(tiem);
        week.setText("星期" + DatetimeUtil.getWhere());
        date.setText(DatetimeUtil.getSysDate("yyyy年MM月dd日"));
        new Thread(new MyThread()).start();
    }

    private void message() {
    }

    private void personnel() {
    }

    private void SetImagView() {
        AutoText autoText = getView(R.id.autoText);
        autoText.startFor0();
        main_gallery = getView(R.id.main_gallery);
        main_lin = getView(R.id.main_lin);
        preview = getView(R.id.preview);
        mSurfaceHolder = preview.getHolder();
        mSurfaceHolder.addCallback(new SufaceListener());
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mSurfaceHolder.setFixedSize(640, 480); //设置分辨率
        imgview = getView(R.id.imgview);
        gallery_adapter = new Gallery_adapter(this);
        main_gallery.setAdapter(gallery_adapter);
        main_gallery.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                return false;
            }
        });


        //设置滚动图片的时候，对应小圆点的图片切换
        main_gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                View v = main_lin.getChildAt(position);
                View cuview = main_lin.getChildAt(current_circle);

                if (v != null && cuview != null) {
                    ImageView pointView = (ImageView) v;
                    ImageView curpointView = (ImageView) cuview;
                    curpointView
                            .setBackgroundResource(R.mipmap.pink);
                    pointView
                            .setBackgroundResource(R.mipmap.red);
                    current_circle = position;
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //
        timeadv = new Runnable() {

            @Override
            public void run() {
                //获取当前的图片是哪一张图片，图片的序号，
                count = main_gallery.getSelectedItemPosition();
                //当前滚动的图片序号大于多有的图片的数量，就跳转到第一张图片，否则就跳转到下一张图片
                if (count + 1 >= li.size()) {
                    count = 0;
                } else {
                    count = count + 1;
                }
                main_gallery.setSelection(count);
                handler.postDelayed(this, 2500);

            }
        };

        //开始定时器，1000毫秒切换一次图片5
        handler.postDelayed(timeadv, 2500);

    }


    private void setView() {
        place = getView(R.id.place);
        set = getView(R.id.set);
        set.setOnClickListener(this);
        place.setText(mSharePreferenceUtil.getClassId());
        listView = getView(R.id.listview);
        practical = getView(R.id.num);
        loadMusic();
        practical.setText("0/0");
        setupViews(R.layout.layout_depar, R.layout.layout_department, R.id.layout_more, R.id.tiee, realmHelper.QueryAll(), 0);

        timer.schedule(task, 1000, 1000);

        //定时器
        Timer timerCard = new Timer();
        TimerTask tastCard = new TimerTask() {
            @Override
            public void run() {
                if (MyApp.getsInstance().getgiDestPort() == 1)
                    MyApp.getsInstance().setCard("1");


            }
        };
        timerCard.schedule(tastCard, 1000, 1000);


    }


    private void GetClass() {
        Api.getInstance().GetMeeting(new AbsAPICallback<AttendanceResutl>(this) {

            @Override
            public void onCompleted() {
            }

            @Override
            public void onNext(AttendanceResutl attendanceResutl) {
                realmHelper.deleteDogAll(MeetingResutl.class);
                realmHelper.deleteDogAll(PersonnelResutl.class);
                li.clear();
                if (attendanceResutl.isCode()) {
                    if (attendanceResutl.getCourse() != null) {
                        mSharePreferenceUtil.setClassId(attendanceResutl.getRoomName());
                        for (int i = 0; i < attendanceResutl.getCourse().size(); i++) {
                            BigenCard.add(attendanceResutl.getCourse().get(i).getBeginTime());
                            EndCard.add(attendanceResutl.getCourse().get(i).getEndTime());
                            CourseId.add(attendanceResutl.getCourse().get(i).getUuid());
                            class_attemdamce.add(attendanceResutl.getCourse().get(i).getClassId());

                        }

                        mSharePreferenceUtil.setIsSameClass(attendanceResutl.getIsSameClass());
                        MyApp.getsInstance().setCourseId(CourseId);
                        MyApp.getsInstance().setBigenCard(BigenCard);
                        MyApp.getsInstance().setEndCard(EndCard);
                        MyApp.getsInstance().setClass_attemdamce(class_attemdamce);
                        for (int i = 0; i < attendanceResutl.getCourse().size(); i++) {
                            MeetingResutl meetingResutl = new MeetingResutl();
                            meetingResutl.setBegin(attendanceResutl.getCourse().get(i).getBeginTime());
                            meetingResutl.setEnd(attendanceResutl.getCourse().get(i).getEndTime());
                            meetingResutl.setUuid(attendanceResutl.getCourse().get(i).getUuid());
                            meetingResutl.setClassId(attendanceResutl.getCourse().get(i).getClassId() + "");
                            meetingResutl.setCourseId(attendanceResutl.getCourse().get(i).getUuid() + "");
                            meetingResutl.setClassName(attendanceResutl.getCourse().get(i).getClassName() + "");
                            meetingResutl.setCourseName(attendanceResutl.getCourse().get(i).getCourseName());
                            meetingResutl.setMainTeacherId(attendanceResutl.getCourse().get(i).getMainTeacherId() + "");
                            meetingResutl.setMainTeacherName(attendanceResutl.getCourse().get(i).getMainTeacherName() + "");
                            if (attendanceResutl.getCourse().get(i).getAttachmentlist() != null && attendanceResutl.getCourse().get(i).getAttachmentlist().size() != 0) {
                                for (int j = 0; j < attendanceResutl.getCourse().get(i).getAttachmentlist().size(); j++) {
                                    Attachment attachment = new Attachment();
                                    attachment.setCourseId(attendanceResutl.getCourse().get(i).getUuid());
                                    attachment.setClassId(attendanceResutl.getCourse().get(i).getClassId());
                                    attachment.setAttachName(attendanceResutl.getCourse().get(i).getAttachmentlist().get(j).getAttachName());
                                    attachment.setAttachUrl(mSharePreferenceUtil.getServerAddress() + attendanceResutl.getCourse().get(i).getAttachmentlist().get(j).getAttachUrl());
                                    realmHelper.addDog(attachment);


                                }
                            }

                            if (attendanceResutl.getCourse().get(i).getCheckRule() != null) {
                                meetingResutl.setBeginTime(attendanceResutl.getCourse().get(i).getBeginTime());
                                meetingResutl.setEndTime(attendanceResutl.getCourse().get(i).getEndTime());
                                meetingResutl.setCheckMode(attendanceResutl.getCourse().get(i).getCheckRule().getCheckMode());
                                meetingResutl.setInBefore(attendanceResutl.getCourse().get(i).getCheckRule().getInBefore());
                                meetingResutl.setNeedCheckout(attendanceResutl.getCourse().get(i).getCheckRule().getNeedCheckout());
                                meetingResutl.setAbsenteeism(attendanceResutl.getCourse().get(i).getCheckRule().getAbsenteeism());
                                meetingResutl.setOutBefore(attendanceResutl.getCourse().get(i).getCheckRule().getOutBefore());
                                meetingResutl.setLeaveEarly(attendanceResutl.getCourse().get(i).getCheckRule().getLeaveEarly());
                                meetingResutl.setOutLate(attendanceResutl.getCourse().get(i).getCheckRule().getOutLate());

                            }
                            realmHelper.addDog(meetingResutl);
                        }

                        for (int i = 0; i < attendanceResutl.getCourse().size(); i++) {
                            for (int j = 0; j < attendanceResutl.getCourse().get(i).getList().size(); j++) {
                                PersonnelResutl personnelResutl = new PersonnelResutl();
                                personnelResutl.setUuid(attendanceResutl.getCourse().get(i).getUuid());
                                personnelResutl.setXm(attendanceResutl.getCourse().get(i).getList().get(j).getXm());
                                personnelResutl.setCardNo(attendanceResutl.getCourse().get(i).getList().get(j).getCardNo());
                                personnelResutl.setType(0);
                                personnelResutl.setTime("00:00:00");
                                personnelResutl.setInResult("4");
                                personnelResutl.setOutResult(null);
                                personnelResutl.setAttendResult("4");
                                personnelResutl.setLg("0");
                                personnelResutl.setClassId(attendanceResutl.getCourse().get(i).getClassId());
                                personnelResutl.setCourseId(attendanceResutl.getCourse().get(i).getUuid() + "");
                                personnelResutl.setWlkh(attendanceResutl.getCourse().get(i).getList().get(j).getFactoryfixId());
                                personnelResutl.setTermCode(attendanceResutl.getCourse().get(0).getScheduleAddress());
                                personnelResutl.setUseState(attendanceResutl.getCourse().get(i).getList().get(j).getUseState());
                                personnelResutl.setUserId(attendanceResutl.getCourse().get(i).getList().get(j).getTraineeId());
                                realmHelper.addDog(personnelResutl);
                            }


                        }
                    }

                    indexBigen = 0;
                    indexEnd = 0;
                    mSharePreferenceUtil.setIndexBigen(indexBigen);
                    mSharePreferenceUtil.setIndexEnd(indexEnd);
                    place.setText(attendanceResutl.getRoomName());
                    setupViews(R.layout.layout_depar, R.layout.layout_department, R.id.layout_more, R.id.tiee, realmHelper.QueryAll(), 0);

                    for (int i = 0; i < realmHelper.QueryImag(CourseId.get(0), class_attemdamce.get(0)).size(); i++) {
                        Attachment attachment = ((Attachment) realmHelper.QueryImag(CourseId.get(0), class_attemdamce.get(0)).get(i));
                        li.add(attachment.getAttachUrl());
                    }

                    if (li.size() != 0) {
                        gallery_adapter.setList(li);
                        setCircle();
                    }

                }
            }

            //
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                Log.i("TAG", "e：" + e.getMessage());

            }


        }, mSharePreferenceUtil.getEquipment(), DatetimeUtil.getSysDate("yyyy-MM-dd HH:mm:ss"));


        //取现在时间
    }

    //设置滚动图片的小圆点
    private void setCircle() {
        main_lin.removeAllViews();
        for (int i = 0; i < li.size(); i++) {
            ImageView iv = new ImageView(MainActivity.this);
            //循环创建小圆点，判断第一个小圆点为白色的，其他的都是透明的
            if (i == 0) {
                iv.setBackgroundResource(R.mipmap.red);
            } else {
                iv.setBackgroundResource(R.mipmap.pink);
            }
            main_lin.addView(iv);
            //设置小圆点的margin值
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);  // , 1�ǿ�ѡд��
            lp.setMargins(5, 10, 5, 10);
            iv.setLayoutParams(lp);
        }
    }

    private void setupViews(int layout1, int layout2, int layout3, int layout4, List<MeetingResutl> list, int type) {

        mMoreLayout = getView(layout3);   //找到该容器（这里的控件为LinearLayout，转换为ViewGroup是因为ViewGroup是容器的基类）
        mMoreLayouts = getView(layout4);
        mMoreLayout.removeAllViews();
        mMoreLayouts.removeAllViews();

        */
/**
         * 由于文字也是动态生成，这里用数组比较多余，使用android中array文件定义资源文件，并取出(便于管理，文字和代码分开，日后要修改直接找资源数组文件便可，不用找代码)
         *//*


        final String[] categoriess = {"班级", "时间", "课程", "教师"};


        */
/**
         * 动态添加布局方法封装
         * 参数 1.父容器    2.资源文字数组  3.从第几个开始   4.行数
         *//*

        if (list.size() != 0) {
//            fillView(mMoreLayout, list, layout2);
            fillViews(mMoreLayout, list, layout2);
        }
//        fillViewsOne(mMoreLayouts, categories, layout1);
        fillViewsOne(mMoreLayouts, categoriess, layout1);

    }

    public void fillViews(ViewGroup layout, List<MeetingResutl> list, int lone) {
        String firstCategory = null;
        String secondCategory = null;
        String thirdCategory = null;
        for (int i = 0; i < 4; i++) {
            if (list.size() == 1) {
                if (i == 0) {
                    firstCategory = list.get(0).getClassName();
                } else if (i == 1) {
                    firstCategory = DatetimeUtil.gethour(list.get(0).getBegin()) + " - " + DatetimeUtil.gethour(list.get(0).getEnd());
                } else if (i == 2) {
                    firstCategory = list.get(0).getCourseName();
                } else if (i == 3) {
                    firstCategory = list.get(0).getMainTeacherName();
                }

            } else if (list.size() == 2) {
                if (i == 0) {

                    firstCategory = list.get(0).getClassName();
                    secondCategory = list.get(1).getClassName();
                } else if (i == 1) {
                    firstCategory = DatetimeUtil.gethour(list.get(0).getBegin()) + " - " + DatetimeUtil.gethour(list.get(0).getEnd());
                    secondCategory = DatetimeUtil.gethour(list.get(1).getBegin()) + " - " + DatetimeUtil.gethour(list.get(1).getEnd());

                } else if (i == 2) {
                    firstCategory = list.get(0).getCourseName();
                    secondCategory = list.get(1).getCourseName();

                } else if (i == 3) {
                    firstCategory = list.get(0).getMainTeacherName();
                    secondCategory = list.get(1).getMainTeacherName();
                }

            } else if (list.size() == 3) {
                if (i == 0) {
                    firstCategory = list.get(0).getClassName();
                    secondCategory = list.get(1).getClassName();
                    thirdCategory = list.get(2).getClassName();

                } else if (i == 1) {
                    firstCategory = DatetimeUtil.gethour(list.get(0).getBegin()) + " - " + DatetimeUtil.gethour(list.get(0).getEnd());
                    secondCategory = DatetimeUtil.gethour(list.get(1).getBegin()) + " - " + DatetimeUtil.gethour(list.get(1).getEnd());
                    thirdCategory = DatetimeUtil.gethour(list.get(2).getBegin()) + " - " + DatetimeUtil.gethour(list.get(2).getEnd());

                }
                if (i == 2) {
                    firstCategory = list.get(0).getCourseName();
                    secondCategory = list.get(1).getCourseName();
                    thirdCategory = list.get(2).getCourseName();

                }
                if (i == 3) {
                    firstCategory = list.get(0).getMainTeacherName();
                    secondCategory = list.get(1).getMainTeacherName();
                    thirdCategory = list.get(2).getMainTeacherName();
                }

            }

            linearLayout = new LinearLayout(this);

            // 表格第一条线
            View.inflate(this, R.layout.layout_line_horizonal, layout);
            View.inflate(this, R.layout.layout_line_vertical, linearLayout);
            // 第一个子布局
            View.inflate(this, lone, linearLayout);
            View.inflate(this, R.layout.layout_line_vertical, linearLayout);
            // 第二个子布局
            View.inflate(this, lone, linearLayout);
            View.inflate(this, R.layout.layout_line_vertical, linearLayout);

            // 第三个子布局
            View.inflate(this, lone, linearLayout);
            View.inflate(this, R.layout.layout_line_vertical, linearLayout);


            layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layout.addView(linearLayout, layoutParams);

            // 表格最后一条线
            View.inflate(this, R.layout.layout_line_horizonal, layout);

            //根据索引getChildAt到指定的位置
            final View firstView = linearLayout.getChildAt(1);
            firstView.setTag(firstCategory);        //设置tag，便于在后面判断点击的哪一个
            final TextView firstTextView = (TextView) firstView.findViewById(R.id.text_title);
            firstTextView.setText(firstCategory);   //设置文字


            final View secondView = linearLayout.getChildAt(3);
            secondView.setTag(secondCategory);
            final TextView secondTextView = (TextView) secondView.findViewById(R.id.text_title);
            secondTextView.setText(secondCategory);

            final View thirdView = linearLayout.getChildAt(5);
            thirdView.setTag(thirdCategory);
            final TextView thirdTextView = (TextView) thirdView.findViewById(R.id.text_title);
            thirdTextView.setText(thirdCategory);


        }
    }


    public void fillViewsOne(ViewGroup layout, String[] categories, int lone) {
        for (int i = 0; i < categories.length; i++) {
            //找到索引，便于根据索引添加图片文件和文字
            String firstCategory = categories[i];
            linearLayout = new LinearLayout(this);
            // 表格第一条线
            View.inflate(this, R.layout.layout_line_horizonal, layout);
            // 第一个子布局
            View.inflate(this, R.layout.layout_line_vertical, linearLayout);
            View.inflate(this, lone, linearLayout);
            View.inflate(this, R.layout.layout_line_vertical, linearLayout);

            layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layout.addView(linearLayout, layoutParams);

            // 表格最后一条线
            View.inflate(this, R.layout.layout_line_horizonal, layout);

            //根据索引getChildAt到指定的位置
            final View firstView = linearLayout.getChildAt(1);
            firstView.setTag(firstCategory);        //设置tag，便于在后面判断点击的哪一个
            final TextView firstTextView = (TextView) firstView.findViewById(R.id.text_title);
            firstTextView.setText(firstCategory);   //设置文字
        }
    }

    @Subscribe(tags = @Tag("card"))
    public void onReadCard(CardInfo cardInfo) {
        Log.d("TAG", "cardInfo:" + cardInfo.getCardID());
        BigenCard = MyApp.getsInstance().getBigenCard();
        MeetingResutl meetingResutl = (MeetingResutl) realmHelper.querMeetingResutll(class_attemdamce.get(indexBigen)).get(0);
        if (BigenCard.size() != 0 && indexBigen < BigenCard.size() && indexEnd < EndCard.size()) {
            String Bigen = DatetimeUtil.getDateTimeFromMillisecond(DatetimeUtil.TimeStrToSecond(BigenCard.get(indexBigen)) - 1000 * 60 * meetingResutl.getInBefore());
            String End = DatetimeUtil.getDateTimeFromMillisecond(
                    DatetimeUtil.TimeStrToSecond(EndCard.get(indexEnd)) + 1000 * 60 * meetingResutl.getLeaveEarly());
            if (realmHelper.isDogExist(cardInfo.getCardID(), CourseId.get(indexBigen), class_attemdamce.get(indexBigen))) {
                if (DatetimeUtil.TimeCompare(DatetimeUtil.getSysDate("HH:mm"), DatetimeUtil.gethour(Bigen), DatetimeUtil.gethour(End))) {
                    List<MeetingCheck> meetingChecks = realmHelper.queryDogList(cardInfo.getCardID(), CourseId.get(indexBigen), class_attemdamce.get(indexBigen));
                    List<MeetingCheck> meetingCheck = realmHelper.queryDogListwlkh(cardInfo.getCardID(), class_attemdamce.get(indexBigen));
                    if (meetingCheck.size() != 0) {
                        if (meetingCheck.get(0).getTime() != null)
                            soundPool2.play(1, 1, 1, 0, 0, 1);
                    } else if (meetingChecks.size() == 1 && meetingResutl.getNeedCheckout() == 0) {
                        //你已签到过
                       if (meetingCheck.get(0).getTime() != null)
                        soundPool2.play(1, 1, 1, 0, 0, 1);
                    } else {
                        if (isSoundReady) {
                            CheckCardExistService.stopAttendance();
                            Date(cardInfo.getCardID());
                            takePicture(cardInfo.getCardID());
                            soundPool.play(1, 1, 1, 0, 0, 1);
                            list = realmHelper.queryDobByAge(CourseId.get(mSharePreferenceUtil.getIndexBigen()), class_attemdamce.get(mSharePreferenceUtil.getIndexBigen()));
                            practical.setText(mSharePreferenceUtil.getRealized() + "/" + list.size());
                            attendanceAdapter.clear();
                            attendanceAdapter.setDate(list);
                            attendanceAdapter.notifyDataSetChanged();

                        }
                    }

                } else {
                    Toast.makeText(MainActivity.this, "您暂时没有需要签到的课程", Toast.LENGTH_SHORT).show();
                    soundPool1.play(1, 1, 1, 0, 0, 1);
                }
            } else {

                soundPool1.play(1, 1, 1, 0, 0, 1);
//                Toast.makeText(MainActivity.this, "您不在该此教室上课", Toast.LENGTH_SHORT).show();
            }
        } else {
            soundPool1.play(1, 1, 1, 0, 0, 1);
        }

    }


    private void UploadImage(String base64, String wlkh, String Userid) {

        Subscriber<ImageResutl> subscriber = new AbsAPICallback<ImageResutl>(this) {
            @Override
            public void onNext(ImageResutl imageResutl) {
                Log.d("TAG", "resutl:" + imageResutl.getMessage());


            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);

            }
        };

        Api.getInstance().UploadImageString(subscriber, "TrainClass", base64, wlkh, Userid);
    }


    private void takePicture(final String whik) {
        if (mCamera != null) {
            mCamera.autoFocus(null);
            mCamera.setPreviewCallback(null);
            mCamera.takePicture(null, null, new Camera.PictureCallback() {
                @Override
                public void onPictureTaken(byte[] bytes, Camera camera) {
                    CheckCardExistService.stopAttendance();
                    bitmap = BitmapFactory.decodeByteArray
                            (bytes, 0, bytes.length);//图片
                    Matrix matrix = new Matrix();
                    matrix.setRotate(90);
                    Bitmap Newbm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                    main_gallery.setVisibility(View.GONE);
                    main_lin.setVisibility(View.GONE);
                    imgview.setVisibility(View.VISIBLE);
                    imgview.setImageBitmap(zoomImg(Newbm, 720, 700));
                    UploadImage(DatetimeUtil.Bitmap2StrByBase64(Newbm), whik, realmHelper.queryDogById(whik).getUserId());
                    timerImgView = new Timer();
                    taskImgView = new TimerTask() {
                        @Override
                        public void run() {
                            // 需要做的事:发送消息  
                            Message message = new Message();
                            message.what = 2;
                            handler.sendMessage(message);
                        }
                    };
                    timerImgView.schedule(taskImgView, 2000);// 1s后执行task,经过1s再次执行
                }
            });
        }
    }

    public Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
        // 获得图片的宽高   
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 计算缩放比例   
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数   
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片   www.2cto.com
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }

    boolean am = true;
    boolean pm = true;
    boolean night = true;
    boolean Fig;
    boolean DataFig;
    String Bigen;
    String End;
    Timer timer = new Timer(true);
    TimerTask task = new TimerTask() {

        @Override
        public void run() {
            handler.sendEmptyMessage(1);
        }
    };

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    if (DatetimeUtil.TimeCompare(DatetimeUtil.getSysDate("HH:mm"), "00:00", "11:59") && am) {
                        GetClass();
                        am = false;
                        pm = true;
                        night = true;


                    }
                    if (DatetimeUtil.TimeCompare(DatetimeUtil.getSysDate("HH:mm"), "12:00", "17:59") && pm) {
                        GetClass();
                        am = true;
                        night = true;
                        pm = false;

                    }
                    if (DatetimeUtil.TimeCompare(DatetimeUtil.getSysDate("HH:mm"), "18:00", "23:59") && night) {
                        GetClass();
                        am = true;
                        night = false;
                        pm = true;


                    }
                    indexBigen = mSharePreferenceUtil.getIndexBigen();
                    indexEnd = mSharePreferenceUtil.getIndexEnd();
                    subscript = mSharePreferenceUtil.getSubscript();
                    Fig = mSharePreferenceUtil.getFig();
                    DataFig = mSharePreferenceUtil.getDataFig();


                    CourseId = MyApp.getsInstance().getCourseId();
                    BigenCard = MyApp.getsInstance().getBigenCard();
                    EndCard = MyApp.getsInstance().getEndCard();


                    if (BigenCard.size() != 0 && indexBigen < BigenCard.size() && indexEnd < EndCard.size()) {
                        if (indexBigen >= BigenCard.size() || indexEnd >= EndCard.size()) {
                            indexBigen = BigenCard.size() - 1;
                            indexEnd = EndCard.size() - 1;
                        }
                        MeetingResutl meetingResutl = (MeetingResutl) realmHelper.querMeetingResutll(class_attemdamce.get(indexBigen)).get(0);

                        Bigen = DatetimeUtil.getDateTimeFromMillisecond(
                                DatetimeUtil.TimeStrToSecond(BigenCard.get(indexBigen)) - 1000 * 60 * meetingResutl.getInBefore());
                        End = DatetimeUtil.getDateTimeFromMillisecond(
                                DatetimeUtil.TimeStrToSecond(EndCard.get(indexEnd)) + 1000 * 60 * meetingResutl.getOutLate());
                        Log.d("TAG", "开始时间：" + Bigen);
                        Log.d("TAG", "结束时间：" + End);
                        Log.d("TAG", "indexBigen：" + indexBigen);
                        Log.d("TAG", "indexEnd：" + indexEnd);
                        Log.d("TAG", "getInBefore：" + meetingResutl.getInBefore());
                        Log.d("TAG", "getOutLate：" + meetingResutl.getOutLate());
                        if (DatetimeUtil.TimeCompare(DatetimeUtil.getSysDate("HH:mm"), DatetimeUtil.gethour(Bigen), DatetimeUtil.gethour(End)) && (!subscript)) {
                            mSharePreferenceUtil.setSubscript(true);
                            mSharePreferenceUtil.setIndexBigen(indexBigen);
                            li.clear();
                            for (int i = 0; i < realmHelper.QueryImag(CourseId.get(0), class_attemdamce.get(0)).size(); i++) {
                                Attachment attachment = ((Attachment) realmHelper.QueryImag(CourseId.get(0), class_attemdamce.get(0)).get(i));
                                li.add(attachment.getAttachUrl());
                            }
                            if (li.size() != 0) {
                                gallery_adapter.setList(li);
                                setCircle();
                            }


                            Log.d("TAG", "getIsSameClass" + mSharePreferenceUtil.getIsSameClass());
                            Log.d("TAG", "getCheckMode:" + meetingResutl.getCheckMode());

                            if (mSharePreferenceUtil.getIsSameClass() == 1 && meetingResutl.getCheckMode() == 2) {
                                if (mSharePreferenceUtil.getIndexBigen() != 0) {
                                    List<MeetingCheck> list2 = realmHelper.querMeetingCheck(CourseId.get(mSharePreferenceUtil.getIndexBigen() - 1), class_attemdamce.get(mSharePreferenceUtil.getIndexBigen() - 1));
                                    if (list2.size() != 0) {
                                        for (int i = 0; i < list2.size(); i++) {
                                            if (list2.get(i).getTime() != null) {
                                                realmHelper.UpdateFindll(CourseId.get(mSharePreferenceUtil.getIndexBigen()), class_attemdamce.get(mSharePreferenceUtil.getIndexBigen()), list2.get(i).getUserId(), DatetimeUtil.getTiem(list2.get(i).getTime()));
                                                Log.d("TAG","TIEM:"+list2.get(i).getTime());
//                                                realmHelper.UpdateFindll(CourseId.get(mSharePreferenceUtil.getIndexBigen()), class_attemdamce.get(mSharePreferenceUtil.getIndexBigen()), list2.get(i).getUserId(), DatetimeUtil.getTiem(list2.get(i).getTime()));

                                            }
                                        }
                                    }
                                }

                            }
                            list = realmHelper.queryDobByAge(CourseId.get(mSharePreferenceUtil.getIndexBigen()), class_attemdamce.get(mSharePreferenceUtil.getIndexBigen()));
                            practical.setText(mSharePreferenceUtil.getRealized() + "/" + list.size());
                            listView.setVisibility(View.VISIBLE);
                            attendanceAdapter = new AttendanceAdapter(MainActivity.this);
                            listView.setAdapter(attendanceAdapter);
//                            attendanceAdapter.clear();
                            attendanceAdapter.setDate(list);
                            realmHelper.UpdateFindllTye(CourseId.get(mSharePreferenceUtil.getIndexBigen()), 0);
                            mSharePreferenceUtil.setFig(false);
                        }


                        Log.d("TAG", "mSharePreferenceUtil.getIsSameClass()  :" + mSharePreferenceUtil.getIsSameClass());
                        MeetingResutl meeting = (MeetingResutl) realmHelper.querMeetingResutll(class_attemdamce.get(0)).get(0);
                        MeetingResutl meetingLast = (MeetingResutl) realmHelper.querMeetingResutll(class_attemdamce.get(class_attemdamce.size() - 1)).get(0);
                        Log.d("TAG", "mSharePreferenceUtil.meeting()  :" + meeting.getCheckMode());
                        Log.d("TAG", "mSharePreferenceUtil.meetingLast()  :" + meetingLast.getCheckMode());
                        if (mSharePreferenceUtil.getIsSameClass() == 1) {
                            if (meeting.getCheckMode() == 2 || meetingLast.getCheckMode() == 2) {
                                if (DatetimeUtil.getSysDate("yyyy-MM-dd HH:mm:ss").equals(DatetimeUtil.getDateTimeFromMillisecond(
                                        DatetimeUtil.TimeStrToSecond(EndCard.get(EndCard.size() - 1)) + 1000 * 60 * meetingResutl.getLeaveEarly()))) {
                                    UpoadCheck();
                                }
                            }
                        }
                        if (DatetimeUtil.getSysDate("yyyy-MM-dd HH:mm:ss").equals(DatetimeUtil.getDateTimeFromMillisecond(
                                DatetimeUtil.TimeStrToSecond(EndCard.get(indexEnd)) + 1000 * 60 * meetingResutl.getLeaveEarly()))) {
                            mSharePreferenceUtil.setSubscript(false);
                            if (meeting.getCheckMode() == 1 && meeting.getCheckMode() == 1) {
                                UploadCheck();
                            }
                            practical.setText(0 + "/" + 0);
                            realized = 0;
                            mSharePreferenceUtil.setRealized(0);
                            indexEnd++;
                            indexBigen++;
                            mSharePreferenceUtil.setIndexEnd(indexEnd);
                            mSharePreferenceUtil.setIndexBigen(indexBigen);
                            listView.setVisibility(View.GONE);
                            attendanceAdapter.clear();

                        }
//                        if (DatetimeUtil.getSysDate("yyyy-MM-dd HH:mm:ss").equals(End) && (!Fig)) {
//
//
//
//                        }

//                        if (DatetimeUtil.TimeCompare(DatetimeUtil.getSysDate("HH:mm"), DatetimeUtil.gethour(End), DatetimeUtil.gethour(DatetimeUtil.getDateTimeFromMillisecond(
//                                DatetimeUtil.TimeStrToSecond(End) + 1000 * 60 * 3))) && (!Fig)) {
//                            UploadCheck(DataFig);
//                            mSharePreferenceUtil.setSubscript(false);
//                            practical.setText(0 + "/" + 0);
//                            mSharePreferenceUtil.setRealized(0);
////                        mSharePreferenceUtil.setIndexEnd(indexEnd);
//                            Log.d("TAG", "3分钟上传");
//                        }

                    }


                    break;

                case 2:

                    CheckCardExistService.startAttendance();
                    bitmap = null;
                    imgview.setImageBitmap(null);
                    main_gallery.setVisibility(View.VISIBLE);
                    main_lin.setVisibility(View.VISIBLE);
                    imgview.setVisibility(View.GONE);
                    if (timerImgView != null) {
                        taskImgView.cancel();
                    }
                    if (timer != null) {
                        task.cancel();
                    }
                    timer = new Timer();
                    task = new TimerTask() {
                        @Override
                        public void run() {
                            // 需要做的事:发送消息  
                            Message message = new Message();
                            message.what = 1;
                            handler.sendMessage(message);
                        }
                    };
                    timer.schedule(task, 1000, 1000);// 1s后执行task,经过1s再次执行

                    break;
                case 3:
                    SetData(tiem);
                    break;

            }
        }
    };

    private void Date(String name) {
        MeetingCheck meetingCheck = new MeetingCheck();
        indexBigen = mSharePreferenceUtil.getIndexBigen();
        list = realmHelper.queryDobByAge(CourseId.get(mSharePreferenceUtil.getIndexBigen()), class_attemdamce.get(mSharePreferenceUtil.getIndexBigen()));
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getWlkh() != null) {
                if (list.get(i).getWlkh().equals(name)) {
                    list.get(i).setTime(DatetimeUtil.getSysDate("HH:mm:ss"));
                    realmHelper.UpdateDog(name, CourseId.get(mSharePreferenceUtil.getIndexBigen()), DatetimeUtil.getSysDate("HH:mm:ss"));
                    meetingCheck.setClassId(list.get(i).getClassId());
                    meetingCheck.setInResult("4");
                    meetingCheck.setTermCode(mSharePreferenceUtil.getClassId());
                    meetingCheck.setLg("0");
                    meetingCheck.setCourseId(list.get(i).getCourseId());
                    meetingCheck.setUserId(list.get(i).getUserId());
                    meetingCheck.setTime(DatetimeUtil.getSysDate("yyyy-MM-dd HH:mm:ss"));
                    Log.d("TAG", "Time:" + meetingCheck.getTime());
                    Log.d("TAG", "getInResult:" + meetingCheck.getInResult());
                    if (meetingCheck.getInResult().equals("1")) {
                        meetingCheck.setAttendResult("1");
                    } else if (meetingCheck.getInResult().equals("2")) {
                        meetingCheck.setAttendResult("2");

                    } else if (meetingCheck.getInResult().equals("4")) {
                        meetingCheck.setAttendResult("4");
                    }
                    meetingCheck.setWlkh(name);
                    if (list.get(i).getType() == 0) {
                        realized++;
                        mSharePreferenceUtil.setRealized(realized);
                    }
                    list.get(i).setType(1);
                    realmHelper.UpdateDogTye(name, CourseId.get(mSharePreferenceUtil.getIndexBigen()), 1);
                    meetingCheck.setOutResult("0");
                    realmHelper.addDog(meetingCheck);

                }
            }
        }

        practical.setText(mSharePreferenceUtil.getRealized() + "/" + list.size() + "");
        attendanceAdapter.notifyDataSetChanged();
    }

    MeetingResutl meetingResutl;

    private void UpoadCheck() {


        for (int j = 0; j < CourseId.size(); j++) {
            List<PersonnelResutl> list = realmHelper.querPersonne(CourseId.get(j));
            MeetingResutl meetingResutlFiall = (MeetingResutl) realmHelper.querMeetingResutl(CourseId.get(j)).get(0);
            for (int i = 0; i < list.size(); i++) {
                if (realmHelper.queryDogMeeting(list.get(i).getCourseId(), list.get(i).getUserId()).size() == 0) {
                    MeetingCheck meetingCheck = new MeetingCheck();
                    meetingCheck.setClassId(list.get(i).getClassId());
                    meetingCheck.setInResult("4");
                    meetingCheck.setCourseId(list.get(i).getCourseId());
                    meetingCheck.setTermCode(mSharePreferenceUtil.getClassId());
                    meetingCheck.setLg("-1");
                    meetingCheck.setTime(null);
                    meetingCheck.setWlkh(list.get(i).getWlkh());
                    meetingCheck.setOutResult("0");
                    meetingCheck.setAttendResult("4");
                    meetingCheck.setUserId(list.get(i).getUserId());
                    realmHelper.addDog(meetingCheck);
                }
                List<MeetingCheck> list2 = realmHelper.querMeeting(class_attemdamce.get(j));
                if (list2.size() != 0) {
                    for (int p = 0; p < list2.size(); p++) {
                        if (list2.get(p).getTime() != null) {
                            realmHelper.UpdateTime(list2.get(p).getClassId(), list2.get(p).getUserId(), list2.get(p).getTime());
                            realmHelper.UpdateFindlll(list2.get(p).getClassId(), list2.get(p).getUserId(), list2.get(p).getTime());


                        }
                    }
                }

                if (meetingResutlFiall.getNeedCheckout() == 1) {
                    if (realmHelper.queryDogMeeting(list.get(i).getCourseId(), list.get(i).getUserId()).size() == 1) {
                        if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime(), BigenCard.get(indexBigen)) <= (meetingResutlFiall.getBeLate() + meetingResutlFiall.getAbsenteeism())) {
                            realmHelper.updateInResultOne(list.get(i).getWlkh(), "1");
                        } else {
                            realmHelper.updateInResultOne(list.get(i).getWlkh(), "2");

                        }
                        if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getTime(), EndCard.get(indexEnd)) <= (meetingResutlFiall.getOutBefore() + meetingResutlFiall.getOutLate())) {
                            realmHelper.updateOutResultOne(list.get(i).getWlkh(), "1");

                        }
                        if (DatetimeUtil.Getminutes(EndCard.get(indexEnd), realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getTime()) <= (meetingResutlFiall.getOutBefore() + meetingResutlFiall.getLeaveEarly())) {
                            realmHelper.updateOutResultOne(list.get(i).getWlkh(), "1");

                        } else {
                            realmHelper.updateOutResult(list.get(i).getWlkh(), "3");

                        }

                        if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("1") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("1")) {
                            realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "1");

                        } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("2") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("1")) {
                            realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "2");

                        } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("1") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("3")) {
                            realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "3");

                        } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("2") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("3")) {
                            realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "5");

                        } else {
                            realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult());
                            realmHelper.updateAttendResult(list.get(i).getWlkh(), realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult());
                        }
                        realmHelper.UpdateLgOne(list.get(i).getWlkh(), "0");  //改状态
                        if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime(), BigenCard.get(indexBigen)) <= (meetingResutlFiall.getBeLate() + meetingResutlFiall.getAbsenteeism())) {
                            realmHelper.updateInResultOne(list.get(i).getWlkh(), "1");

                        } else {
                            realmHelper.updateInResultOne(list.get(i).getWlkh(), "2");
                        }
                    } else if (realmHelper.queryDogMeeting(list.get(i).getCourseId(), list.get(i).getUserId()).size() >= 2) {


                        realmHelper.UpdateLgLate(list.get(i).getWlkh());
                        realmHelper.UpdateLgOne(list.get(i).getWlkh(), "0");
                        if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime(), BigenCard.get(indexBigen)) <= (meetingResutlFiall.getBeLate() + meetingResutlFiall.getAbsenteeism())) {
                            realmHelper.updateInResultOne(list.get(i).getWlkh(), "1");
                            realmHelper.updateInResult(list.get(i).getWlkh(), "1");
                        } else {
                            realmHelper.updateInResultOne(list.get(i).getWlkh(), "2");
                            realmHelper.updateInResult(list.get(i).getWlkh(), "2");
                        }
                        if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getTime(), EndCard.get(indexEnd)) <= (meetingResutlFiall.getOutBefore() + meetingResutlFiall.getOutLate())) {
                            realmHelper.updateOutResultOne(list.get(i).getWlkh(), "1");
                            realmHelper.updateOutResult(list.get(i).getWlkh(), "1");
                        }
                        if (DatetimeUtil.Getminutes(EndCard.get(indexEnd), realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getTime()) <= (meetingResutlFiall.getOutBefore() + meetingResutlFiall.getLeaveEarly())) {
                            realmHelper.updateOutResultOne(list.get(i).getWlkh(), "1");
                            realmHelper.updateOutResult(list.get(i).getWlkh(), "1");
                        } else {
                            realmHelper.updateOutResult(list.get(i).getWlkh(), "3");
                            realmHelper.updateOutResultOne(list.get(i).getWlkh(), "3");
                        }


                        if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("1") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("1")) {
                            realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "1");
                            realmHelper.updateAttendResult(list.get(i).getWlkh(), "1");
                        } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("2") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("1")) {
                            realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "2");
                            realmHelper.updateAttendResult(list.get(i).getWlkh(), "2");
                        } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("1") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("3")) {
                            realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "3");
                            realmHelper.updateAttendResult(list.get(i).getWlkh(), "3");
                        } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("2") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("3")) {
                            realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "5");
                            realmHelper.updateAttendResult(list.get(i).getWlkh(), "5");
                        } else {
                            realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult());
                            realmHelper.updateAttendResult(list.get(i).getWlkh(), realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult());
                        }


                    }

//                    if (CourseId.size() == 2) {
//                        realmHelper.updateAttendcourseId(list.get(i).getWlkh(), CourseId.get(0) + "," + CourseId.get(1));
//                    } else if (CourseId.size() == 3) {
//                        realmHelper.updateAttendcourseId(list.get(i).getWlkh(), CourseId.get(0) + "," + CourseId.get(1) + "," + CourseId.get(2));
//
//                    }
                    if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime() == null) {
                        realmHelper.UpdateLgOne(list.get(i).getWlkh(), "-1");
                        realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "4");
                    }

                    if (realmHelper.queryDogMeeting(list.get(i).getCourseId(), list.get(i).getUserId()).size() >= 2) {
                        list1.add(realmHelper.QueryTraineeIdOne(list.get(i).getUserId(), list.get(i).getCourseId(), 1));
                        list1.add(realmHelper.QueryTraineeIdOne(list.get(i).getUserId(), list.get(i).getCourseId(), 2));
                    } else if (realmHelper.queryDogMeeting(list.get(i).getCourseId(), list.get(i).getUserId()).size() == 1 || realmHelper.queryDogMeeting(list.get(i).getClassId(), list.get(i).getUserId()).size() == 0) {
                        list1.add(realmHelper.QueryTraineeIdOne(list.get(i).getUserId(), list.get(i).getCourseId(), 1));
                    }

                } else {
                    if (realmHelper.queryDogMeeting(list.get(i).getCourseId(), list.get(i).getUserId()).size() >= 1) {     //    迟到分钟加缺勤分钟
                        if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime(), BigenCard.get(indexBigen)) <= (meetingResutlFiall.getBeLate() + meetingResutlFiall.getAbsenteeism())) {

                            realmHelper.updateInResultOne(list.get(i).getWlkh(), "1");
                            realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "1");
                        } else {
                            realmHelper.updateInResultOne(list.get(i).getWlkh(), "2");
                            realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "2");

                        }
                        if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime() == null) {
                            realmHelper.UpdateLgOne(list.get(i).getWlkh(), "-1");
                            realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "4");
                        }
                        realmHelper.UpdateLgOne(list.get(i).getWlkh(), "0");  //改状态
                        realmHelper.updateOutResultOne(list.get(i).getWlkh(), "1");

                        if (CourseId.size() == 2 && mSharePreferenceUtil.getIsSameClass() == 0) {
                            realmHelper.updateAttendcourseId(list.get(i).getWlkh(), CourseId.get(0) + "," + CourseId.get(1));
                        } else if (CourseId.size() == 3 && mSharePreferenceUtil.getIsSameClass() == 0) {
                            realmHelper.updateAttendcourseId(list.get(i).getWlkh(), CourseId.get(0) + "," + CourseId.get(1) + "," + CourseId.get(2));
                        }


                        if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime() == null) {
                            realmHelper.UpdateLgOne(list.get(i).getWlkh(), "-1");
                            realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "4");
                        }
                        if (class_attemdamce.get(0).equals(class_attemdamce.get(1))) {


                        }
                        list1.add(realmHelper.QueryTraineeIdOne(list.get(i).getUserId(), list.get(i).getCourseId(), 1));

                    }


                }
            }
        }
        String json = JSON.toJSONString(list1);
        Log.d("TAG", "list1:" + list1.size());
        Log.d("TAG", "json:" + json);
        Subscriber subscr = new AbsAPICallback<Resutl>(this) {
            @Override
            public void onCompleted() {
                Log.d("TAG", "上传失败");
                listView.setVisibility(View.GONE);
            }


            @Override
            public void onNext(Resutl resul) {
                Log.d("TAG", "上传成功");
                listView.setVisibility(View.GONE);
                Log.d("TAG", "时间：" + (DatetimeUtil.getDateTimeFromMillisecond(
                        DatetimeUtil.TimeStrToSecond(EndCard.get(EndCard.size() - 1)) + 1000 * 60 * meetingResutl.getLeaveEarly())));
                if (resul.isCode()) {
                    realmHelper.deleteDogAll(MeetingCheck.class);
                    realmHelper.deleteDogMeeting(list.get(indexBigen - 1).getCourseId(), list.get(indexBigen - 1).getClassId());
                    list1.clear();
                    mSharePreferenceUtil.setRealized(0);
                }

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                Log.i("TAG", "e：" + e.getMessage());
                Log.d("TAG", "上传失败");
            }
        };
        Api.getInstance().UploadCheck(subscr, json);

    }


    //如果有2条以上或者2条 而且开启签退 取第一条和最后一条，不然取第一条
    private void UploadCheck() {
        meetingResutl = (MeetingResutl) realmHelper.querMeetingResutll(CourseId.get(mSharePreferenceUtil.getIndexBigen()), class_attemdamce.get(mSharePreferenceUtil.getIndexBigen())).get(0);
        BigenCard = MyApp.getsInstance().getBigenCard();
        CourseId = MyApp.getsInstance().getCourseId();
        if (list.size() != 0) {
            list = realmHelper.queryDobByAge(CourseId.get(mSharePreferenceUtil.getIndexBigen()), class_attemdamce.get(mSharePreferenceUtil.getIndexBigen()));
            if (meetingResutl.getCheckMode() == 1) {
                for (int i = 0; i < list.size(); i++) {
                    if (realmHelper.QueryTraineeId(list.get(i).getUserId(), list.get(i).getCourseId()).size() == 0) {
                        MeetingCheck meetingCheck = new MeetingCheck();
                        meetingCheck.setClassId(list.get(i).getClassId());
                        meetingCheck.setInResult("4");
                        meetingCheck.setCourseId(list.get(i).getCourseId());
                        meetingCheck.setTermCode(mSharePreferenceUtil.getClassId());
                        meetingCheck.setLg("-1");
                        meetingCheck.setTime(null);
                        meetingCheck.setWlkh(list.get(i).getWlkh());
                        meetingCheck.setOutResult("0");
                        meetingCheck.setAttendResult("4");
                        meetingCheck.setUserId(list.get(i).getUserId());
                        realmHelper.addDog(meetingCheck);
                    }
                    if (mSharePreferenceUtil.getNeedCheckout() == 1) {
                        if (realmHelper.QueryTraineeId(list.get(i).getUserId(), list.get(i).getCourseId()).size() == 1) {
                            if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime(), BigenCard.get(indexBigen)) <= (meetingResutl.getOutBefore() + meetingResutl.getAbsenteeism())) {
                                realmHelper.updateInResultOne(list.get(i).getWlkh(), "1");
                            } else {
                                realmHelper.updateInResultOne(list.get(i).getWlkh(), "2");

                            }
                            if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getTime(), EndCard.get(indexEnd)) <= (meetingResutl.getOutBefore() + meetingResutl.getOutLate())) {
                                realmHelper.updateOutResultOne(list.get(i).getWlkh(), "1");

                            }

                            if (DatetimeUtil.Getminutes(EndCard.get(indexEnd), realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getTime()) <= (meetingResutl.getOutBefore() + meetingResutl.getLeaveEarly())) {
                                realmHelper.updateOutResultOne(list.get(i).getWlkh(), "1");

                            } else {
                                realmHelper.updateOutResult(list.get(i).getWlkh(), "3");

                            }

                            if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("1") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("1")) {
                                realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "1");

                            } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("2") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("1")) {
                                realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "2");

                            } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("1") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("3")) {
                                realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "3");

                            } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("2") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("3")) {
                                realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "5");

                            } else {
                                realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult());
                                realmHelper.updateAttendResult(list.get(i).getWlkh(), realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult());
                            }
                            realmHelper.UpdateLgOne(list.get(i).getWlkh(), "0");  //改状态
                            if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime(), BigenCard.get(indexBigen)) <= (meetingResutl.getBeLate() + meetingResutl.getAbsenteeism())) {
                                realmHelper.updateInResultOne(list.get(i).getWlkh(), "1");

                            } else {
                                realmHelper.updateInResultOne(list.get(i).getWlkh(), "2");
                            }
                            if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime() == null) {
                                realmHelper.UpdateLgOne(list.get(i).getWlkh(), "-1");
                                realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "4");
                            }
//

                        } else if (realmHelper.QueryTraineeId(list.get(i).getUserId(), list.get(i).getCourseId()).size() >= 2) {
                            Log.d("TAG", "2");
                            realmHelper.UpdateLgLate(list.get(i).getWlkh());
                            realmHelper.UpdateLgOne(list.get(i).getWlkh(), "0");
                            if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime(), BigenCard.get(indexBigen)) <= (meetingResutl.getBeLate() + meetingResutl.getAbsenteeism())) {
                                realmHelper.updateInResultOne(list.get(i).getWlkh(), "1");
                                realmHelper.updateInResult(list.get(i).getWlkh(), "1");
                            } else {
                                realmHelper.updateInResultOne(list.get(i).getWlkh(), "2");
                                realmHelper.updateInResult(list.get(i).getWlkh(), "2");
                            }
                            if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getTime(), EndCard.get(indexEnd)) <= (meetingResutl.getOutBefore() + meetingResutl.getOutLate())) {
                                realmHelper.updateOutResultOne(list.get(i).getWlkh(), "1");
                                realmHelper.updateOutResult(list.get(i).getWlkh(), "1");
                            }
                            if (DatetimeUtil.Getminutes(EndCard.get(indexEnd), realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getTime()) <= (meetingResutl.getOutBefore() + meetingResutl.getLeaveEarly())) {
                                realmHelper.updateOutResultOne(list.get(i).getWlkh(), "1");
                                realmHelper.updateOutResult(list.get(i).getWlkh(), "1");
                            } else {
                                realmHelper.updateOutResult(list.get(i).getWlkh(), "3");
                                realmHelper.updateOutResultOne(list.get(i).getWlkh(), "3");
                            }

                            if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("1") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("1")) {
                                realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "1");
                                realmHelper.updateAttendResult(list.get(i).getWlkh(), "1");
                            } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("2") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("1")) {
                                realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "2");
                                realmHelper.updateAttendResult(list.get(i).getWlkh(), "2");
                            } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("1") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("3")) {
                                realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "3");
                                realmHelper.updateAttendResult(list.get(i).getWlkh(), "3");
                            } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("2") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("3")) {
                                realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "5");
                                realmHelper.updateAttendResult(list.get(i).getWlkh(), "5");
                            } else {
                                realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult());
                                realmHelper.updateAttendResult(list.get(i).getWlkh(), realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult());
                            }

                        }
                        if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime() == null) {
                            realmHelper.UpdateLgOne(list.get(i).getWlkh(), "-1");
                            realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "4");
                        }

                        if (realmHelper.QueryTraineeId(list.get(i).getUserId(), list.get(i).getCourseId()).size() >= 2) {
                            list1.add(realmHelper.QueryTraineeIdOne(list.get(i).getUserId(), list.get(i).getCourseId(), 1));
                            list1.add(realmHelper.QueryTraineeIdOne(list.get(i).getUserId(), list.get(i).getCourseId(), 2));
                        } else if (realmHelper.QueryTraineeId(list.get(i).getUserId(), list.get(i).getCourseId()).size() == 1 || realmHelper.QueryTraineeId(list.get(i).getUserId(), list.get(i).getCourseId()).size() == 0) {
                            list1.add(realmHelper.QueryTraineeIdOne(list.get(i).getUserId(), list.get(i).getCourseId(), 1));
                        }
                        Log.d("TAG", "开启签退");
                    } else {
                        if (realmHelper.QueryTraineeId(list.get(i).getUserId(), list.get(i).getCourseId()).size() >= 1) {
                            if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime(), BigenCard.get(indexBigen)) <= (meetingResutl.getBeLate() + meetingResutl.getAbsenteeism())) {
                                realmHelper.updateInResultOne(list.get(i).getWlkh(), "1");
                                realmHelper.updateAttendResult(list.get(i).getWlkh(), "1");
                            } else {
                                realmHelper.updateInResultOne(list.get(i).getWlkh(), "2");
                                realmHelper.updateAttendResult(list.get(i).getWlkh(), "2");

                            }
                        }
                        realmHelper.updateOutResultOne(list.get(i).getWlkh(), "1");
                        realmHelper.UpdateLgOne(list.get(i).getWlkh(), "0");  //改状态
                        if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime() == null) {
                            realmHelper.UpdateLgOne(list.get(i).getWlkh(), "-1");
                            realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "4");
                        }


                        list1.add(realmHelper.QueryTraineeIdOne(list.get(i).getUserId(), list.get(i).getCourseId(), 1));
                        Log.d("TAG", "没开启签退");
                    }

                }
            } else {
                if (mSharePreferenceUtil.getIsSameClass() == 0) {
                    for (int i = 0; i < list.size(); i++) {
                        if (realmHelper.queryDogList(list.get(i).getWlkh(), list.get(i).getUserId()).size() == 0) {
                            MeetingCheck meetingCheck = new MeetingCheck();
                            meetingCheck.setClassId(list.get(i).getClassId());
                            meetingCheck.setInResult("4");
                            meetingCheck.setCourseId(list.get(i).getCourseId());
                            meetingCheck.setTermCode(mSharePreferenceUtil.getClassId());
                            meetingCheck.setLg("-1");
                            meetingCheck.setTime(null);
                            meetingCheck.setWlkh(list.get(i).getWlkh());
                            meetingCheck.setOutResult("0");
                            meetingCheck.setAttendResult("4");
                            meetingCheck.setUserId(list.get(i).getUserId());
                            realmHelper.addDog(meetingCheck);

                        }
                        if (meetingResutl.getNeedCheckout() == 1) {
                            if (realmHelper.queryDogList(list.get(i).getWlkh(), list.get(i).getUserId()).size() == 1) {

                                if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime(), BigenCard.get(indexBigen)) <= (meetingResutl.getBeLate() + meetingResutl.getAbsenteeism())) {
                                    realmHelper.updateInResultOne(list.get(i).getWlkh(), "1");
                                } else {
                                    realmHelper.updateInResultOne(list.get(i).getWlkh(), "2");

                                }
                                if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getTime(), EndCard.get(indexEnd)) <= (meetingResutl.getOutBefore() + meetingResutl.getOutLate())) {
                                    realmHelper.updateOutResultOne(list.get(i).getWlkh(), "1");

                                }
                                if (DatetimeUtil.Getminutes(EndCard.get(indexEnd), realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getTime()) <= (meetingResutl.getOutBefore() + meetingResutl.getLeaveEarly())) {
                                    realmHelper.updateOutResultOne(list.get(i).getWlkh(), "1");

                                } else {
                                    realmHelper.updateOutResult(list.get(i).getWlkh(), "3");

                                }

                                if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("1") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("1")) {
                                    realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "1");

                                } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("2") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("1")) {
                                    realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "2");

                                } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("1") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("3")) {
                                    realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "3");

                                } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("2") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("3")) {
                                    realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "5");

                                } else {
                                    realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult());
                                    realmHelper.updateAttendResult(list.get(i).getWlkh(), realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult());
                                }
                                realmHelper.UpdateLgOne(list.get(i).getWlkh(), "0");  //改状态
                                if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime(), BigenCard.get(indexBigen)) <= (meetingResutl.getBeLate() + meetingResutl.getAbsenteeism())) {
                                    realmHelper.updateInResultOne(list.get(i).getWlkh(), "1");

                                } else {
                                    realmHelper.updateInResultOne(list.get(i).getWlkh(), "2");
                                }
                                if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime() == null) {
                                    realmHelper.UpdateLgOne(list.get(i).getWlkh(), "-1");
                                    realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "4");
                                }
//


                            } else if (realmHelper.queryDogList(list.get(i).getWlkh(), list.get(i).getUserId()).size() >= 2) {


                                realmHelper.UpdateLgLate(list.get(i).getWlkh());
                                realmHelper.UpdateLgOne(list.get(i).getWlkh(), "0");
                                if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime(), BigenCard.get(indexBigen)) <= (meetingResutl.getBeLate() + meetingResutl.getAbsenteeism())) {
                                    realmHelper.updateInResultOne(list.get(i).getWlkh(), "1");
                                    realmHelper.updateInResult(list.get(i).getWlkh(), "1");
                                } else {
                                    realmHelper.updateInResultOne(list.get(i).getWlkh(), "2");
                                    realmHelper.updateInResult(list.get(i).getWlkh(), "2");
                                }
                                if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getTime(), EndCard.get(indexEnd)) <= (meetingResutl.getOutBefore() + meetingResutl.getOutLate())) {
                                    realmHelper.updateOutResultOne(list.get(i).getWlkh(), "1");
                                    realmHelper.updateOutResult(list.get(i).getWlkh(), "1");
                                }
                                if (DatetimeUtil.Getminutes(EndCard.get(indexEnd), realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getTime()) <= (meetingResutl.getOutBefore() + meetingResutl.getLeaveEarly())) {
                                    realmHelper.updateOutResultOne(list.get(i).getWlkh(), "1");
                                    realmHelper.updateOutResult(list.get(i).getWlkh(), "1");
                                } else {
                                    realmHelper.updateOutResult(list.get(i).getWlkh(), "3");
                                    realmHelper.updateOutResultOne(list.get(i).getWlkh(), "3");
                                }


                                if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("1") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("1")) {
                                    realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "1");
                                    realmHelper.updateAttendResult(list.get(i).getWlkh(), "1");
                                } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("2") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("1")) {
                                    realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "2");
                                    realmHelper.updateAttendResult(list.get(i).getWlkh(), "2");
                                } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("1") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("3")) {
                                    realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "3");
                                    realmHelper.updateAttendResult(list.get(i).getWlkh(), "3");
                                } else if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult().equals("2") && realmHelper.queryDogByIdLast(list.get(i).getWlkh()).getOutResult().equals("3")) {
                                    realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "5");
                                    realmHelper.updateAttendResult(list.get(i).getWlkh(), "5");
                                } else {
                                    realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult());
                                    realmHelper.updateAttendResult(list.get(i).getWlkh(), realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getInResult());
                                }


                            }

                            if (CourseId.size() == 2) {
                                realmHelper.updateAttendcourseId(list.get(i).getWlkh(), CourseId.get(0) + "," + CourseId.get(1));
                            } else if (CourseId.size() == 3) {
                                realmHelper.updateAttendcourseId(list.get(i).getWlkh(), CourseId.get(0) + "," + CourseId.get(1) + "," + CourseId.get(2));

                            }
                            if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime() == null) {
                                realmHelper.UpdateLgOne(list.get(i).getWlkh(), "-1");
                                realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "4");
                            }

                            if (realmHelper.queryDogList(list.get(i).getWlkh(), list.get(i).getUserId()).size() >= 2) {
                                list1.add(realmHelper.QueryTraineeId(list.get(i).getUserId(), list.get(i).getClassId(), 1));
                                list1.add(realmHelper.QueryTraineeId(list.get(i).getUserId(), list.get(i).getClassId(), 2));
                            } else if (realmHelper.queryDogList(list.get(i).getWlkh(), list.get(i).getUserId()).size() == 1 || realmHelper.queryDogList(list.get(i).getWlkh(), list.get(i).getUserId()).size() == 0) {
                                list1.add(realmHelper.QueryTraineeId(list.get(i).getUserId(), list.get(i).getClassId(), 1));
                            }

                        } else {
                            if (realmHelper.queryDogList(list.get(i).getWlkh(), list.get(i).getUserId()).size() >= 1) {     //    迟到分钟加缺勤分钟
                                if (DatetimeUtil.Getminutes(realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime(), BigenCard.get(indexBigen)) <= (meetingResutl.getBeLate() + meetingResutl.getAbsenteeism())) {
                                    realmHelper.updateInResultOne(list.get(i).getWlkh(), "1");
                                    realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "1");
                                } else {
                                    realmHelper.updateInResultOne(list.get(i).getWlkh(), "2");
                                    realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "2");

                                }

                                realmHelper.UpdateLgOne(list.get(i).getWlkh(), "0");  //改状态
                                realmHelper.updateOutResultOne(list.get(i).getWlkh(), "1");

                                if (CourseId.size() == 2) {
//                                    meetingCheck.setCourseId(CourseId.get(0)+","+CourseId.get(1));
                                    realmHelper.updateAttendcourseId(list.get(i).getWlkh(), CourseId.get(0) + "," + CourseId.get(1));
                                } else if (CourseId.size() == 3) {
                                    realmHelper.updateAttendcourseId(list.get(i).getWlkh(), CourseId.get(0) + "," + CourseId.get(1) + "," + CourseId.get(2));
                                }
                                if (realmHelper.queryDogByIdOne(list.get(i).getWlkh()).getTime() == null) {
                                    realmHelper.UpdateLgOne(list.get(i).getWlkh(), "-1");
                                    realmHelper.updateAttendResultOne(list.get(i).getWlkh(), list.get(i).getUserId(), "4");
                                }

                                list1.add(realmHelper.QueryTraineeId(list.get(i).getUserId(), list.get(i).getClassId(), 1));

                            }


                        }
                    }
                    class_attemdamce = MyApp.getsInstance().getClass_attemdamce();
                }
            }


        }


        mSharePreferenceUtil.setDataFig(true);
        String json = JSON.toJSONString(list1);
        Log.d("TAG", "json:" + json);
        Log.d("TAG", "list1:" + list1.size());
        Subscriber subscr = new AbsAPICallback<Resutl>(this) {
            @Override
            public void onCompleted() {
                Log.d("TAG", "上传失败");
            }


            @Override
            public void onNext(Resutl resul) {
                Log.d("TAG", "上传成功");
                Log.d("TAG", "上resul:"+resul);
                if (resul.isCode()) {
                    if (DatetimeUtil.getSysDate("yyyy-MM-dd HH:mm:ss").equals(DatetimeUtil.getDateTimeFromMillisecond(
                            DatetimeUtil.TimeStrToSecond(EndCard.get(EndCard.size() - 1)) + 1000 * 60 * meetingResutl.getLeaveEarly()))) {
                        realmHelper.deleteDogAll(MeetingCheck.class);
                    }
                    realmHelper.deleteDogMeeting(CourseId.get(indexBigen - 1), class_attemdamce.get(indexBigen - 1));

                    list1.clear();
                    mSharePreferenceUtil.setRealized(0);
                    mSharePreferenceUtil.setFig(false);
                    mSharePreferenceUtil.setDataFig(false);

                }

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                Log.i("TAG", "e：" + e.getMessage());
                Log.d("TAG", "上传失败");
            }
        };
        Api.getInstance().UploadCheck(subscr, json);


    }


    private class SufaceListener implements SurfaceHolder.Callback {
        private boolean isPreview;

        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            try {
                WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
                Display display = wm.getDefaultDisplay();
                //int num = mCamera.getNumberOfCameras();
                mCamera = Camera.open(0);
                Camera.Parameters parameters = mCamera.getParameters();
                List<Camera.Size> vSizeList = parameters.getSupportedPictureSizes();

                for (int num = 0; num < vSizeList.size(); num++) {
                    Camera.Size vSize = vSizeList.get(num);
                }

                if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
                    //如果是竖屏
                    mCamera.setDisplayOrientation(90);
                } else {
                    mCamera.setDisplayOrientation(180);
                }
                parameters.setPictureFormat(PixelFormat.JPEG);
                mCamera.setParameters(parameters);
                mCamera.setPreviewDisplay(mSurfaceHolder);
                mCamera.startPreview();
                isPreview = true;

            } catch (Exception e) {
                Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                Log.d("TAG", "e:" + e.getMessage());
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
            if (mSurfaceHolder == null) {
                // preview surface does not exist
                return;
            }

            // stop preview before making changes
            try {
                if (mCamera != null) {
                    mCamera.stopPreview();

                }

            } catch (Exception e) {
                // ignore: tried to stop a non-existent preview
            }

            try {
                mCamera.setPreviewDisplay(mSurfaceHolder);
                mCamera.startPreview();

            } catch (Exception e) {

            }
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            if (mCamera != null) {
                if (isPreview) mCamera.stopPreview();
                mCamera.release();
                mCamera = null;
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //销毁activity之前先关闭定时器
        if (handler != null) {
            if (timeadv != null) {
                handler.removeCallbacks(timeadv);
            }
        }
        RxBus.get().unregister(this);


    }

    private void loadMusic() {
        soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 5);
        soundPool1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 5);
        soundPool2 = new SoundPool(10, AudioManager.STREAM_MUSIC, 5);
        failure = soundPool1.load(this, R.raw.failure, 0);
        sourceid = soundPool.load(this, R.raw.media, 0);
        credit = soundPool2.load(this, R.raw.credit, 0);
        soundPool
                .setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {

                    public void onLoadComplete(SoundPool soundPool,
                                               int sampleId, int status) {
                        isSoundReady = true;

                    }
                });
        soundPool1
                .setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {

                    public void onLoadComplete(SoundPool soundPool,
                                               int sampleId, int status) {
                        isSoundReady = true;

                    }
                });
        soundPool2
                .setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {

                    public void onLoadComplete(SoundPool soundPool,
                                               int sampleId, int status) {
                        isSoundReady = true;

                    }
                });


    }


    private void SetData(TextView tiem) {
        tiem.setText(DatetimeUtil.getSysDate("HH:mm:ss"));
    }

    public class MyThread implements Runnable {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(1000);//线程暂停1秒，单位毫秒
                    Message message = new Message();
                    message.what = 3;
                    handler.sendMessage(message);//发送消息
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }


    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.set:
                SetDiagFragment setDiagFragment = new SetDiagFragment();
                setDiagFragment.show(getFragmentManager(), SetDiagFragment.class.getName());

                break;
        }
    }


    private <VT extends View> VT getView(@IdRes int id) {
        return (VT) findViewById(id);
    }


}
*/
